package com.kapcb.assemble.chain;

import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.function.BiConsumer;

/**
 * <a>Title: ChainBuilder </a>
 * <a>Author: Kapcb <a>
 * <a>Description: ChainBuilder <a>
 *
 * @author Kapcb
 * @version 1.0
 * @date 2024/4/18 20:41
 * @since 1.0
 */
public class ChainBuilder<T extends ChainRequest, R extends ChainResponse> {

    private int cnt = 0;

    private BiConsumer<T, R> targetMethod;

    private List<Chain<T, R>> chainList = Lists.newArrayList();

    private ChainBuilder() {
    }

    public static <T extends ChainRequest, R extends ChainResponse> ChainBuilder newBuilder() {
        return new ChainBuilder<T, R>();
    }

    public ChainBuilder<T, R> addChain(Chain<T, R> chain) {
        if (Objects.isNull(chain)) {
            throw new IllegalArgumentException("chain can not be null, pls check your code!");
        }

        this.chainList.add(chain);
        this.cnt++;
        return this;
    }

    public ChainBuilder<T, R> addChains(List<Chain<T, R>> chains) {
        if (CollectionUtils.isEmpty(chains)) {
            throw new IllegalArgumentException("chain can not be null, pls check your code!");
        }

        this.chainList.addAll(chains);
        this.cnt = this.chainList.size();
        return this;
    }

    public ChainBuilder<T, R> addTargetMethod(BiConsumer<T, R> targetMethod) {
        this.targetMethod = targetMethod;
        return this;
    }

    public Chain<T, R> build() {
        HandlerExecutionChain<T, R> handlerExecutionChain = HandlerExecutionChain.newHandlerExecutionChain();
        return null;
    }

}
